package org.budo.support.slf4j;

import org.budo.support.lang.util.ReflectUtil;
import org.budo.support.slf4j.logger.NullLogger;
import org.budo.support.slf4j.logger.console.BudoSlf4jConsoleLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.NOPLogger;

/**
 * @author limingwei
 */
public class Slf4j {
    public static Logger getLogger() {
        String name = Thread.currentThread().getStackTrace()[2].getClassName();
        return getLogger(name);
    }

    public static Logger getLogger(Class<?> type) {
        return getLogger(type.getName());
    }

    public static Logger getLogger(String type) {
        Logger logger = LoggerFactory.getLogger(type);
        if (!(logger instanceof NOPLogger)) {
            return logger;
        }

        if (ReflectUtil.hasClass("org.slf4j.impl.StaticLoggerBinder")) {
            return logger;
        }

        // 获取到空logger，并没有slf4j log实现
        return new BudoSlf4jConsoleLogger(type);
    }

    public static Logger nullLogger() {
        return new NullLogger();
    }

    public static Logger console() {
        String name = Thread.currentThread().getStackTrace()[2].getClassName();
        Logger logger = getLogger(name);

        return new BudoSlf4jConsoleLogger(name,logger);
    }
}